package com.example.crm01.service.impl.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.crm01.entity.Business;
import com.example.crm01.entity.system.ServicePackage;
import com.example.crm01.mapper.BusinessMapper;
import com.example.crm01.mapper.system.ServicePackageMapper;
import com.example.crm01.service.BusinessService;
import com.example.crm01.service.ServicePackageService;
import com.example.crm01.service.form.BaseIdForm;
import com.example.crm01.service.form.system.ServicePackageAddForm;
import com.example.crm01.service.form.system.ServicePackageListForm;
import com.example.crm01.utils.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
public class ServicePackageServiceImpl extends ServiceImpl<ServicePackageMapper, ServicePackage> implements ServicePackageService {

    public final ServicePackageMapper servicePackageMapper;

    public ServicePackageServiceImpl(ServicePackageMapper servicePackageMapper) {
        this.servicePackageMapper = servicePackageMapper;
    }

    @Override
    public Page<ServicePackage> listSystemBusiness(ServicePackageListForm form) {
        Page<ServicePackage> page = new Page<>(form.getPageNum(), form.getPageSize());

        LambdaQueryWrapper<ServicePackage> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(form.getName())) {
            queryWrapper.like(ServicePackage::getName, form.getName());
        }

        if (StringUtils.isNotEmpty(form.getRegionName()) && ObjectUtils.isNotEmpty(form.getRegionId())) {
            queryWrapper.like(ServicePackage::getRegionName, form.getRegionName());
            queryWrapper.like(ServicePackage::getRegionId, form.getRegionId());
        }

        queryWrapper.ne(ServicePackage::getDel_flag, 2);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Boolean addServicePackage(ServicePackageAddForm form) {
        ServicePackage servicePackage = new ServicePackage();
        servicePackage.setName(form.getName());
        servicePackage.setRegionId(form.getRegionId());
        servicePackage.setRegionName(form.getRegionName());
        servicePackage.setPrice(form.getPrice());
        servicePackage.setServicePackageNum(form.getServicePackageNum());

        //-----
        servicePackage.setCreateBy("1");
        servicePackage.setCreateTime(LocalDateTime.now());
        servicePackage.setUpdateBy("1");
        servicePackage.setUpdateTime(LocalDateTime.now());
        servicePackage.setDel_flag(0);
        int insert = servicePackageMapper.insert(servicePackage);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean updateServicePackage(ServicePackageAddForm form) {
        if (ObjectUtils.isEmpty(form.getId())){
            throw new RuntimeException("修改套餐信息，主键id不能为空");
        }
        ServicePackage servicePackage = servicePackageMapper.selectById(form.getId());
        servicePackage.setName(form.getName());
        servicePackage.setRegionId(form.getRegionId());
        servicePackage.setRegionName(form.getRegionName());
        servicePackage.setPrice(form.getPrice());
        servicePackage.setServicePackageNum(form.getServicePackageNum());

        //-----
        //todo 有一个bug
        servicePackage.setUpdateBy("1");
        servicePackage.setUpdateTime(LocalDateTime.now());
        servicePackage.setDel_flag(1);
        int insert = servicePackageMapper.updateById(servicePackage);
        if (insert > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean deleteServicePackage(BaseIdForm form) {
        ServicePackage servicePackage = servicePackageMapper.selectById(form.getId());
        if (ObjectUtils.isNotEmpty(servicePackage)){
            throw new RuntimeException("套餐信息不存在删除失败");
        }
        servicePackage.setUpdateBy("1");
        servicePackage.setUpdateTime(LocalDateTime.now());
        servicePackage.setDel_flag(2);
        int update = servicePackageMapper.updateById(servicePackage);
        if (update > 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
